package com.nj.dms.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.nj.dms.bean.Essay;
import com.nj.dms.bean.PaperMoldDocument;
import com.nj.dms.dao.PaperMoldDocumentDao;
import com.nj.dms.util.DbUtil;

public class PaperMoldDocumentDaoImpl implements PaperMoldDocumentDao {

	@Override
	public boolean addPaperMoldDocument(PaperMoldDocument paperMoldDocument) {

		// 连接数据库
		Connection conn = DbUtil.dbOpen();

		PreparedStatement pstmt = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		int i = 0;

		try {
			pstmt = conn.prepareStatement(
					"insert into paper_mold_document(user_name,title,description,thick,audit,roll_num,publish_date,update_date,location) values(?,?,?,?,?,?,?,?,?)");

			pstmt.setString(1, paperMoldDocument.getUserName());
			pstmt.setString(2, paperMoldDocument.getTitle());
			pstmt.setString(3, paperMoldDocument.getDescription());
			pstmt.setInt(4, paperMoldDocument.getThick());
			pstmt.setInt(5, paperMoldDocument.getAudit());
			pstmt.setString(6, paperMoldDocument.getRollNum());
			pstmt.setString(7, sdf.format(paperMoldDocument.getPublishDate()));
			pstmt.setString(8, sdf.format(paperMoldDocument.getUpdateDate()));
			pstmt.setString(9, paperMoldDocument.getLocation());

			i = pstmt.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtil.dbClose(conn, pstmt);
		}

		return i > 0;
	}

	@Override
	public boolean savePaperMoldDocument(PaperMoldDocument paperMoldDocument) {

		Connection conn = DbUtil.dbOpen();

		PreparedStatement pstmt = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		int i = 0;

		try {
			pstmt = conn.prepareStatement(
					"update paper_mold_document set title=?, description=?, thick=?, audit=?, roll_num=?, update_date=?, location=? where id=?");

			pstmt.setString(1, paperMoldDocument.getTitle());
			pstmt.setString(2, paperMoldDocument.getDescription());
			pstmt.setInt(3, paperMoldDocument.getThick());
			pstmt.setInt(4, paperMoldDocument.getAudit());
			pstmt.setString(5, paperMoldDocument.getRollNum());
			pstmt.setString(6, sdf.format(paperMoldDocument.getUpdateDate()));
			pstmt.setString(7, paperMoldDocument.getLocation());
			pstmt.setInt(8, paperMoldDocument.getId());

			i = pstmt.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {

			DbUtil.dbClose(conn, pstmt);

		}

		return i > 0;
	}

	@Override
	public boolean deletePaperMoldDocument(int id) {
		// 删除文章
		Connection conn = DbUtil.dbOpen();
		PreparedStatement pstmt = null;
		int i = 0;

		try {
			pstmt = conn.prepareStatement("delete from paper_mold_document where id=?");

			pstmt.setInt(1, id);

			i = pstmt.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtil.dbClose(conn, pstmt);
		}

		return i > 0;
	}

	@Override
	public boolean deletePaperMoldDocument(String[] ids) {

		Connection con = DbUtil.dbOpen();
		// 定义一个int类型的数组，保存数据增加、删除、修改的每一批次返回的影响记录数
		int[] i = null;
		PreparedStatement pst = null;

		// String sql="delete from user where userid in( 1,2,3 ) ";

		String sql = "delete from paper_mold_document where id=?";
		try {
			// 调用Connection类的setAutoCommit(false)方法,将自动提交该为手动提交方式
			con.setAutoCommit(false);

			pst = con.prepareStatement(sql);
			for (int j = 0; j < ids.length; j++) {
				// 调用Integer包装类的parseInt(String),将数字字符串转换为整数
				pst.setInt(1, Integer.parseInt(ids[j]));
				// 调用PreparedStatement类的addBatch()方法，保存一次数据处理的有参数的sql语句
				pst.addBatch();
			}
			// 调用PreparedStatement类的executeBatch()方法，写入数据
			i = pst.executeBatch();
			// 正式向数据库写入数据
			con.commit();

		} catch (Exception e) {
			e.getMessage();
		} finally {
			DbUtil.dbClose(con, pst);
		}
		return i.length > 0;
	}

	@Override
	public PaperMoldDocument findPaperMoldDocument(int id) {

		Connection conn = DbUtil.dbOpen();

		ResultSet rs = null;

		PreparedStatement pstmt = null;

		PaperMoldDocument paperMoldDocument = new PaperMoldDocument();

		try {
			// 查询一个文章
			pstmt = conn.prepareStatement(
					"select id, user_name, title, description,  thick, audit, roll_num,  publish_date, update_date, location from paper_mold_document where id=?");

			pstmt.setInt(1, id);

			rs = pstmt.executeQuery();

			if (rs != null && rs.next()) {

				paperMoldDocument.setId(rs.getInt("id"));
				paperMoldDocument.setUserName(rs.getString("user_name"));
				paperMoldDocument.setTitle(rs.getString("title"));
				paperMoldDocument.setDescription(rs.getString("description"));
				paperMoldDocument.setThick(rs.getInt("thick"));
				paperMoldDocument.setAudit(rs.getInt("audit"));
				paperMoldDocument.setRollNum(rs.getString("roll_num"));
				paperMoldDocument.setPublishDate(new Date(rs.getTimestamp("publish_date").getTime()));
				paperMoldDocument.setUpdateDate(new Date(rs.getTime("update_date").getTime()));
				paperMoldDocument.setLocation(rs.getString("location"));

			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {

			DbUtil.dbClose(conn, pstmt, rs);

		}

		return paperMoldDocument;
	}

	@Override
	public List<PaperMoldDocument> findAllPaperMoldDocument() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<PaperMoldDocument> findAllPaperMoldDocumentByUserByPage(String userName, Integer beginIndex,
			int maxSize) {

		// 查找一个用户的所有文章

		List<PaperMoldDocument> list = new ArrayList();

		// 连接数据库
		Connection conn = DbUtil.dbOpen();

		PreparedStatement pstmt = null;
		ResultSet rs = null;

		try {
			pstmt = conn.prepareStatement(
					"select id, title, roll_num, thick, audit, publish_date from paper_mold_document where user_name=? LIMIT ?,?");

			pstmt.setString(1, userName);
			pstmt.setInt(2, beginIndex);
			pstmt.setInt(3, maxSize);

			rs = pstmt.executeQuery();

			if (rs != null) {

				while (rs.next()) {

					PaperMoldDocument paperMoldDocument = new PaperMoldDocument();
					paperMoldDocument.setId(rs.getInt("id"));
					paperMoldDocument.setTitle(rs.getString("title"));
					paperMoldDocument.setRollNum(rs.getString("roll_num"));
					paperMoldDocument.setThick(rs.getInt("thick"));
					paperMoldDocument.setAudit(rs.getInt("audit"));
					paperMoldDocument.setPublishDate(new Date(rs.getTimestamp("publish_date").getTime()));
					list.add(paperMoldDocument);

				}

			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtil.dbClose(conn, pstmt, rs);
		}

		return list;
	}

	@Override
	public int countPaperMoldDocumentsByUser(String userName) {
		Connection conn = DbUtil.dbOpen();
		ResultSet rs = null;
		PreparedStatement pstmt = null;
		int recordNums = 0;
		try {
			// 统计记录总数方法:select count(id) from user
			String sql = "SELECT COUNT(id) AS num FROM paper_mold_document WHERE user_name=?";

			pstmt = conn.prepareStatement(sql);

			pstmt.setString(1, userName);

			rs = pstmt.executeQuery();
			if (rs.next()) {
				recordNums = rs.getInt("num");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtil.dbClose(conn, pstmt, rs);
		}
		return recordNums;
	}

}
